package com.tastespot.component;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tastespot.mapper.ShopMapper;
import com.tastespot.model.entity.Shop;
import org.redisson.api.RBloomFilter;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import static com.tastespot.constants.RedisConstants.CACHE_SHOP_KEY;

/**
 * @Author: 369pro
 * @CreateTime: 2025-05-17
 * @Version: 1.0
 */

@Component
public class BloomLoader implements ApplicationRunner {

    private final RBloomFilter<String> bloomFilter;
    private final ShopMapper shopMapper;

    public BloomLoader(RBloomFilter<String> bloomFilter,
                       ShopMapper shopMapper) {
        this.bloomFilter = bloomFilter;
        this.shopMapper = shopMapper;
    }

    @Override
    public void run(ApplicationArguments args) {
        int page = 1, size = 1000;
        IPage<Shop> pageInfo;
        do {
            pageInfo = shopMapper.selectPage(
                    new Page<>(page, size),
                    new LambdaQueryWrapper<Shop>().select(Shop::getId)
            );
            pageInfo.getRecords().forEach(shop ->
                    bloomFilter.add(CACHE_SHOP_KEY + shop.getId())
            );
            page++;
        } while (pageInfo.getRecords().size() == size);

    }
}

